package top.coolrainly.module.pb.service.theme;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;
import top.coolrainly.module.pb.controller.admin.theme.vo.*;
import top.coolrainly.module.pb.convert.theme.ThemeConvert;
import top.coolrainly.module.pb.dal.dataobject.theme.ThemeDO;
import top.coolrainly.framework.common.pojo.PageResult;
import top.coolrainly.framework.common.pojo.PageParam;
import top.coolrainly.framework.common.util.object.BeanUtils;

import top.coolrainly.module.pb.dal.mysql.theme.ThemeMapper;

import static top.coolrainly.framework.common.exception.util.ServiceExceptionUtil.exception;
import static top.coolrainly.module.pb.enums.ErrorCodeConstants.*;

/**
 * 党建 主题 Service 实现类
 *
 * @author CoolRainLy
 */
@Service
@Validated
public class ThemeServiceImpl implements ThemeService {

    @Resource
    private ThemeMapper themeMapper;

    @Override
    @Transactional
    public Long createTheme(ThemeSaveReqVO createReqVO) {
        // 插入
        ThemeDO theme = ThemeConvert.INSTANCE.convert(createReqVO);
        themeMapper.insert(theme);

        // 更新网格数据的唯一 ID
        theme.getGrid().setI(theme.getId());
        themeMapper.updateById(theme);

        // 返回
        return theme.getId();
    }

    @Override
    public void updateTheme(ThemeSaveReqVO updateReqVO) {
        // 校验存在
        validateThemeExists(updateReqVO.getId());
        // 更新
        ThemeDO updateObj = ThemeConvert.INSTANCE.convert(updateReqVO);
        themeMapper.updateById(updateObj);
    }

    @Override
    public void deleteTheme(Long id) {
        // 校验存在
        validateThemeExists(id);
        // 删除
        themeMapper.deleteById(id);
    }

    private void validateThemeExists(Long id) {
        if (themeMapper.selectById(id) == null) {
            throw exception(THEME_NOT_EXISTS);
        }
    }

    @Override
    public ThemeDO getTheme(Long id) {
        return themeMapper.selectById(id);
    }

    @Override
    public List<ThemeDO> getThemeList() {
        return themeMapper.selectList();
    }

}